package com.base.project;

import android.os.Build;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ResourceUtils;
import com.blankj.utilcode.util.StringUtils;
import com.tencent.mmkv.MMKV;

/**
 * TranslateHelper 翻译助手
 */
public class TH {

    private static final MMKV mmkv =  MMKV.mmkvWithID("TH");

    public interface OnHtmlAClickListener {
        //回调富文本里 a 标签的 href
        void onClick(String href);
    }

    public static void clearAll(){
        mmkv.clearAll();
    }

    public static void put(String k, String v) {
        mmkv.encode(k,v);
    }

    public static String getString(String key) {
        String v = mmkv.decodeString(key);
        if (v == null) {
            // 未初始化成功时读取本地res
            final int stringInt = ResourceUtils.getStringIdByName(key);
            v = StringUtils.getString(stringInt);
            if (v.equals(String.valueOf(stringInt))) {
                return "";
            }
        }
        return v;
    }

    public static String getString(String key, Object... formatArgs) {
        String text = getString(key);
        if (formatArgs != null && formatArgs.length > 0) {
            text = String.format(text, formatArgs);
        }
        return text;
    }

    public static Spanned htmlDecode(String key) {
        return htmlDecode(key, null, new Object[]{});
    }

    public static Spanned htmlDecode(String key, Object... formatArgs) {
        String text = getString(key, formatArgs);
        Spanned htmlSpanned = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            htmlSpanned = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY);
        } else {
            htmlSpanned = Html.fromHtml(text);
        }
        return htmlSpanned;
    }

    /**
     * 根据key获取 mipmap ID
     *
     * @param key
     * @return
     */
    public static int getMipmapIdByName(String key) {
        String mipmapName = TH.getString(key);
        if (TextUtils.isEmpty(mipmapName)) {
            return 0;
        } else {
            return ResourceUtils.getMipmapIdByName(mipmapName);
        }
    }

    /**
     * 根据key获取 Drawable ID
     *
     * @param key
     * @return
     */
    public static int getDrawableIdByName(String key) {
        String drawableName = TH.getString(key);
        if (TextUtils.isEmpty(drawableName)) {
            return 0;
        } else {
            return ResourceUtils.getDrawableIdByName(drawableName);
        }
    }

    /**
     * 读取a 标签 设置点击事件 并回调 url https://blog.csdn.net/u013077428/article/details/126585549 https://www.jianshu.com/p/d3bef8449960
     * 需要setMovementMethod setAutoLinkMask -> setText
     *
     * @param htmlSpanned
     * @param listener
     * @return
     */
    public static Spanned addClickListener(Spanned htmlSpanned, int textColor, OnHtmlAClickListener listener) {
        if (listener != null) {
            SpannableStringBuilder htmlBuilder = new SpannableStringBuilder(htmlSpanned);
            URLSpan[] htmlSpans = htmlSpanned.getSpans(0, htmlSpanned.length(), URLSpan.class);
            for (URLSpan span : htmlSpans) {
                int start = htmlBuilder.getSpanStart(span);
                int end = htmlBuilder.getSpanEnd(span);
                int flags = htmlBuilder.getSpanFlags(span);
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        String url = span.getURL();
                        listener.onClick(url);
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        //超链接颜色
                        ds.setColor(textColor);
                        //设置是否要下划线
                        ds.setUnderlineText(true);
                    }
                };
                htmlBuilder.setSpan(clickableSpan, start, end, flags);
                htmlSpanned = htmlBuilder;
            }
        }
        return htmlSpanned;
    }
}
